layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    //执行一个laydate实例
    laydate.render({
      elem: '#condition' //指定元素
    });

    /**
     * 抽盘表管理
     */
    var PandianChoupan = {
        tableId: "pandianChoupanTable"
    };

    /**
     * 抽盘明细表管理
     */
    var PandianChoupanDetailed = {
        tableId: "pandianChoupanDetailedTable"
    };

    /**
     * 初始化表格的列
     */
    PandianChoupan.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'diskNumber', sort: true, title: '抽盘日期', style:'font-weight: 600;'},
            {field: 'warehouseName', sort: true, title: '仓库名称', style:'font-weight: 600;'},
            {field: 'createTime', sort: true, title: '创建时间', style:'font-weight: 600;'},
            {field: 'createUserName', sort: true, title: '创建用户', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    PandianChoupan.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        table.reload(PandianChoupan.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    PandianChoupan.exportExcel = function () {
        var checkRows = table.checkStatus(PandianChoupan.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PandianChoupan.tableId,
        url: Feng.ctxPath + '/pandianChoupan/list',
        page: true,
        limit: 20,
        height: 350,
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: PandianChoupan.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PandianChoupan.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        PandianChoupan.exportExcel();
    });

    /**
     * 初始化表格的列
     */
    PandianChoupanDetailed.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'inventory', sort: true, title: '状态',
            	templet: function(d){
            		var str = "";
            		if (d.inventory == '待处理') {
                    	str += '<span style="font-weight: bold;color:#e84d74;">' + d.inventory + '</span>'
					} else if (d.inventory == '已处理'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.inventory + '</span>'
					} else if (d.inventory == '已盘'){
                    	str += '<span style="color:#00c300;font-weight: bold;">' + d.inventory + '</span>'
					} 
            		return str
            	}
			},
            {field: 'panType', sort: true, title: '类型',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.panType + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.panType + '</span>'
					} 
            		return str
            	}
			},
            {field: 'warehouseName', sort: true, title: '仓库', style:'font-weight: 600;'},
            {field: 'areaName', sort: true, title: '库区', style:'font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: '库位', style:'font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: '托盘', style:'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: '商品', style:'font-weight: 600;'},
            {field: 'goodsName', sort: true, title: '商品名称', style:'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: '货号', style:'font-weight: 600;'},
            {field: 'goodsPacking', sort: true, title: '包装', style:'font-weight: 600;'},
            {field: 'goodsSpecs', sort: true, title: '规格', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'num', sort: true, title: '库存数', totalRow: true, style:'font-weight: 600;'},
            {field: 'trueNum', sort: true, title: '盘点数', totalRow: true,
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.trueNum + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.trueNum + '</span>'
					} 
            		return str
            	}
			},
            {field: 'goodsbatchs', sort: true, title: '原批次', style:'font-weight: 600;'},
            {field: 'newgoodsbatchs', sort: true, title: '新批次',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.newgoodsbatchs + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.newgoodsbatchs + '</span>'
					} 
            		return str
            	}
			},
            {field: 'createTime', sort: true, title: '盘点时间',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.createTime + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.createTime + '</span>'
					} 
            		return str
            	}
			},
            {field: 'createUsername', sort: true, title: '盘点人员',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.createUsername + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.createUsername + '</span>'
					} 
            		return str
            	}
			},
            {field: 'pandianNodes', title: '盘点描述',
            	templet: function(d){
            		var str = "";
            		if (d.panType == '正常') {
                    	str += '<span style="font-weight: bold;color:#00c300;">' + d.pandianNodes + '</span>'
					} else {
                    	str += '<span style="color:#e84d74;font-weight: bold;">' + d.pandianNodes + '</span>'
					} 
            		return str
            	}
			},
            {field: 'dealPerson', sort: true, title: '盘点处理人'},
            {field: 'dealTime', sort: true, title: '盘点处理时间'},
            {field: 'nodes', title: '处理描述'},
        ]];
    };

    // 单击行事件
    table.on('row(' + PandianChoupan.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + PandianChoupan.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + PandianChoupan.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        pandianChoupanDetailed(data);
    });
    
    /*//查询库区
    form.on('select(warehouseId)',function(){
    	$("#areaId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicArea/Querylist',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"areaWarehouseId":$("#warehouseId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>请选择</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.areaName+"</option>";
    	        	});
    	        	$("#areaId").html(str);
    	        }
    	    }
    	});
        form.render('select');
    });
    
    //查询库位
    form.on('select(areaId)',function(){
    	$("#positionId").empty();
    	$.ajax({
    		url:Feng.ctxPath + '/basicPosition/Querylist',
    	    dataType:"json",   //返回格式为json
    	    async: false,
    	    data:{"positionAreaId":$("#areaId").val()},    //参数值
    	    type:"post",   //请求方式
    	    success:function(data){
    	        if(data.length>0){
    	        	var str = "<option value=''>请选择</option>";
    	        	$.each(data,function(key,value){
    	        		str += "<option value='"+value.id+"'>"+value.positionNumber+"</option>";
    	        	});
    	        	$("#positionId").html(str);
    	        }
    	    }
    	});
    	form.render('select');
    });*/
    
    function pandianChoupanDetailed(data){
    	var diskId;
		var warehouseId;
    	if (data!=null) {
    		diskId = data.id;
    		warehouseId = data.warehouseId;
		} else {
			diskId = 0;
    		warehouseId = 0;
		}
        // 渲染商品明细表格
        var tableResult = table.render({
            elem: '#' + PandianChoupanDetailed.tableId,
            url: Feng.ctxPath + '/pandianChoupanDetailed/list?diskId=' + diskId+"&warehouseId="+warehouseId,
            page: true,
            limits:[60,120,240,360,480,1500,5000,10000,20000,80000],
            height: 400,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            totalRow: true ,//开启合计行
            cols: PandianChoupanDetailed.initColumn()
        });

        // 搜索按钮点击事件
        $('#btnSearchDetailed').click(function () {
        	PandianChoupanDetailed.search();
        });

        /**
         * 点击查询按钮
         */
    	PandianChoupanDetailed.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            queryData['createUsername'] = $("#createUsername").val();
            queryData['areaName'] = $("#areaName").val();
            queryData['positionNumber'] = $("#positionNumber").val();
            /*queryData['warehouseId'] = $("#warehouseId").val();
            queryData['areaId'] = $("#areaId").val();
            queryData['positionId'] = $("#positionId").val();*/
            table.reload(PandianChoupanDetailed.tableId, {
                where: queryData, page: {curr: 1},
                 done:function () {
			        // 搜索按钮点击事件
			        $('#btnSearchDetailed').click(function () {
			        	PandianChoupanDetailed.search();
			        });
                }
            });
        };

	    /**
	     * 导出excel按钮
	     */
	    PandianChoupanDetailed.exportExcel = function () {
	        var checkRows = table.checkStatus(PandianChoupanDetailed.tableId);
	        if (checkRows.data.length === 0) {
	            Feng.error("请选择要导出的数据");
	        } else {
	            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
	        }
	    };
	
	    // 导出excel
	    $('#btnExpDetailed').click(function () {
	        PandianChoupanDetailed.exportExcel();
	    });
    }

});
